var appPath = getAppPath();

$(function(){
	// 来源
	$("#queryFbFrom").combobox({
		url:appPath+'/yjxFeedback/getFbFrom4Combo.action?blkFlg=1',
		valueField:'from_value',
		textField:'from_text',
		onSelect:function(row){
			// TODO
		}
	});
	var data = [{'id':'0','name':'[不限]'},{'id':'10','name':'APP意见问题反馈'},{'id':'11','name':'创意征集'},{'id':'20','name':'App错误捕获信息'},{'id':'30','name':'线上活动填写反馈'}];
	$("#queryFbType").combobox({
		data : data,
		valueField : 'id',
		textField : 'name',
		onLoadSuccess:function(){
			$("#queryFbType").combobox('select',0).combobox('enable');
		}
	});
	
	initJspGrid();
});

// 样版列表初始化
function initJspGrid(){
	
	$('#feedbackGrid').datagrid({
	    url:appPath + "/yjxFeedback/getFeedbackInfoPg.action",
	    height:'full',
	    striped:true,
	    fitColumns:true,
	    queryParams: getQueryParam(),
	    singleSelect:false,
	    selectOnCheck:true,
	    checkOnSelect:true,
	    frozenColumns:[[
	        {field:'ck',checkbox:'true',width:10,align:'center',sortable:false}
	    ]],
	    columns:[[
  	        {field:'fb_id',title:'反馈ID',width:20,align:'center',sortable:false},
	        {field:'state',title:'状态',width:20,align:'center',sortable:false,
  	        	formatter:function(value, rowData, rowIndex){
	        		if(!rowData.fb_id) return "N/A";
	        		var show = getState4Show(value);
	        		return "<a href='javascript:void(0)' onClick='editState("+rowData.fb_id+")'>"+show+"</a>";
	        	}
		    },
	        {field:'fb_type',title:'反馈类型',width:50,align:'center',sortable:false,
		    	  formatter:function(value, rowData, rowIndex){
		    		  	var ret = '<span>未知类型</spna>';
		    		  	if (value){
		    		  		switch (value || '10'){
		    		  		case 10: ret = '<span style="color:#FFA500;">APP意见问题反馈</spna>'; break;
		    		  		case 11: ret = '<span style="color:#43CD80;">创意征集</spna>'; break;	
		    		  		case 20: ret = '<span style="color:#4682B4;">App错误捕获信息</spna>'; break;
		    		  		case 30: ret = '<span style="color:#43CD80;">线上活动填写反馈</spna>'; break;
		    		  		}
		    		  	}
		    		  return ret;
		    	  }
		    },
		    {field:'from',title:'反馈来源',width:50,align:'center',sortable:false,
		    	  formatter:function(value, rowData, rowIndex){
		    		  if (value){
		    			  return value;
		    		  }
		    		  return '未知';
		    	  }
		    },
	        {field:'detail',title:'反馈详情 [点击查看]',width:50,align:'left',sortable:false,
	        	formatter:function(value, rowData, rowIndex){
	        		if(!rowData.fb_id) return "N/A";
	        		var show = '[点击查看]';
	        		if(rowData.advice && rowData.advice.length>0){
	        			if(rowData.advice.length>16){
	        				show = rowData.advice.substr(0,15)+'...';
	        			}else{
	        				show = rowData.advice+'...';
	        			}
	        		}
	        		return "<a href='javascript:void(0)' onClick='showDetail("+rowData.fb_id+")'>"+show+"</a>";
	        	}
		    },
		    {field:'name',title:'姓名',width:30,align:'center',sortable:false,
	        	formatter:function(value,rowData,rowIndex){
		    		return value||rowData.user_nickname;
		    	}
	        },
	        {field:'user_id',title:'用户ID',width:25,align:'center',sortable:false},
	        {field:'mobile',title:'联系电话',width:40,align:'center',sortable:false,
	        	formatter:function(value,rowData,rowIndex){
		    		if(value){
		    			return value;
		    		}
		    		return '-';
		    	}
	        },
	        {field:'wechat',title:'用户微信',width:40,align:'center',sortable:false,
	        	formatter:function(value,rowData,rowIndex){
		    		if(value){
		    			return value;
		    		}
		    		return '-';
		    	}
	        },
	        {field:'mail',title:'邮箱',width:40,align:'center',sortable:false,
		    	formatter:function(value,rowData,rowIndex){
		    		if(value){
		    			return value;
		    		}
		    		return '<span style="color:#CDCDB4;font-size:10px;">暂无</span>';
		    	}
	        },
	        {field:'tel',title:'注册手机',width:40,align:'center',sortable:false,
	        	formatter:function(value,rowData,rowIndex){
		    		if(value){
		    			return value;
		    		}
		    		return '<span style="color:#CDCDB4;font-size:10px;">暂无</span>';
		    	}
	        },
	        {field:'create_time',title:'创建时间',width:60,align:'left',sortable:false},
	        {field:'update_time',title:'更新时间',width:60,align:'left',sortable:false}
	    ]],
	    hideColumn:['user_type'],
	    pagination:true,
		rownumbers:true,
		toolbar:[
			{
				text:'删除反馈',
				iconCls:'icon-cancel',
				handler:function(){
					delFeedback();
				}
			}
		]
	});
}

function getState4Show(value){
	var state = '<span style="color:red">未知</span>';
	switch(Number(value||'0')){
		case 1: state="未处理"; break;
		case 2: state="处理中"; break;
		case 3: state="已处理"; break;
		case 4: state="垃圾"; break;
	}
	return state;
}
function getFrom4Show(value){
	var from = '<span style="color:red">未知</span>';
	if(value && (value+'').length>0) {
		if (value == 'app') {
			from = 'APP';
		} else if(value == 'evnt_171114'){
			from = '[2017.11大家提需求]'
		} else if(value == 'event_161022'){
			from = '[2016.10找茬活动]'
		} else if(value == 'event_170613'){
			from = '[2017.06找茬活动]'
		}
	}
	return from;  
}

function showDetail(fb_id){
	$("#detailTable").show();
	$('#detailForm').form('clear');
	$("#fbId").val(fb_id);
	$("#fb_from").html('');
	$("#fb_userId").html('');
	$("#fb_userName").html('');
	$("#fb_mobile").html('');
	$("#fb_mail").html('');
	$("#fb_advice").val('');
	$("#fb_device_user").val('');
	$("#fb_device_sys").val('');
	$('#fb_images').html('');
	$("#fb_Name").html('');
	var url = appPath + "/yjxFeedback/getFeedbackInfoList.action?fb_id="+ fb_id;
	$.get(url,function(ret){
		if (ret && ret.length>0){
			var info = ret[0];
			var UNKNOWN = '<span style="color:red">未知</span>';
			$("#fb_from").html(getFrom4Show(info.from));
			$("#fb_userId").html(info.user_id||UNKNOWN);
			$("#fb_userNickname").html(info.user_nickname||UNKNOWN);
			console.log(info.name);
			$("#fb_Name").html(info.name||UNKNOWN);
			$("#fb_mobile").html(info.mobile||UNKNOWN);
			$("#fb_weChat").html(info.wechat||UNKNOWN);
			$("#fb_mail").html(info.mail||UNKNOWN);
			$("#fb_advice").val(info.advice||'');
			$("#fb_device_user").val(info.device_info_user||'');
			$("#fb_device_sys").val(info.device_info_sys||'');
			
			var arrImg = JSON.parse(info.images||'[]');
			if(arrImg && arrImg.length>0){
				var IMG_IDX = 0;
				for(var i=0; i<arrImg.length; i++){
					if(arrImg[i] && arrImg[i].length>0) {
						IMG_IDX = Number(IMG_IDX||0)+1;
			    		
			    		var showImg = arrImg[i]+'?x-oss-process=image/resize,m_pad,h_180,w_180,color_cccccc';
						var imgHtml = '<span style="padding:0;" id="fbImg_'+IMG_IDX+'">'+
							'<a href="' + arrImg[i] + '" target="_blank" class="editImgUrl" style="padding:0;">'+
							'<img src="'+showImg+
							'" style="display:inline;height:100px;width:100px;border:1px solid darkgrey;margin:8px;">'+
							'</a></span>';
						$("#fb_images").append(imgHtml);

						if(IMG_IDX % 4 == 0) {
							$("#fb_images").append('<br>');
						}
					}
				}
			}else{
				$('#fb_images').html('<span style="color:red">无</span>');
			}
		}
	});
	
	$("#detailBtn").show();
	$('#detailDlg').window({
		title:'反馈详情',
		iconCls:'icon-edit',
		modal: true,
		shadow: true,
		collapsible:false,
		minimizable:false,
		maximizable:false
	});
	
	$('#detailDlg').window('move',{top:4,left:20});
	$('#detailDlg').window('open');
}

function closeDetail(){
	$('#detailDlg').window('close');
}

// 状态编辑
function editState(fb_id){
	$("#stateDlg").show();
	$('#stateForm').form('clear');
	$("#updFbId").val(fb_id);
	$("#updFbNote").val('');
	
	var url = appPath + "/yjxFeedback/getFeedbackInfoList.action?fb_id="+ fb_id;
	$.get(url,function(ret){
		if (ret && ret.length>0){
			var info = ret[0];
			$("#updFbState").combobox('select',info.state||0);
			$("#updFbNote").val(info.note||'');
		}
	});
	
	$("#stateBtn").show();
	$('#stateDlg').window({
		title:'反馈状态',
		iconCls:'icon-edit',
		modal: true,
		shadow: true,
		collapsible:false,
		minimizable:false,
		maximizable:false
	});
	
	$('#stateDlg').window('move',{top:20,left:20});
	$('#stateDlg').window('open');
}

function cancelUpdFb(){
	$('#stateDlg').window('close');
}
function doUpdFb(){
	var id = $("#updFbId").val();
	var state = $("#updFbState").combobox('getValue');
	var note = $("#updFbNote").val();
	
	var url = appPath+"/yjxFeedback/updFeedback.action"
	var sendData = {
		"yjxFeedbackInfo.fb_id": Number(id||"0"),
		"yjxFeedbackInfo.state": Number(state || "0"),
		"yjxFeedbackInfo.note": note||''
	};
    $.post(url, sendData, function(data){  
		if (data == "ok") { 
			$.messager.alert('提示信息','提交成功','info');
			$('#stateDlg').window('close');
			doQuery();
        } else {
        	$.messager.alert('提示信息','提交失败','info');
        }
    });
}

function delFeedback(){
	var selectItem=$("#feedbackGrid").datagrid("getChecked");
	if(!selectItem || selectItem.length == 0){
		$.messager.alert('提示信息','请选择反馈项目！','warning');
		return;
	}
	for(var i=0; i<selectItem.length; i++){
		if(Number(selectItem[i].state||'0')==2){
			$.messager.alert('提示信息','不能删除[处理中]反馈！','warning');
			return;
		}
	}
	
	$.messager.confirm("确认", "是否确认[删除]所选用户反馈？",
		function(r){
	        if (r) { 
				var ids = "";
				$.each(selectItem, function(index, item){
					ids = ids + item.fb_id + ",";
				});
				var url = appPath+"/yjxFeedback/delFeedback.action?ids="+ids;
				$.post(url,function(data){
					if(data=="ok"){
						$.messager.alert('提示信息','删除成功','info');
						doQuery();
					}else{
						$.messager.alert('提示信息','删除失败','info');
					}
				});
	        }
		}
	);
}

/**
 * 获取请求信息
 */
function getQueryParam(){
	var queryObject = {};
	
	var findState = $("#queryFbState").combobox('getValue');
	var findFrom = $("#queryFbFrom").combobox('getValue');
	var findMobile = $("#queryFbMobile").val();
	var findFebType = $("#queryFbType").combobox('getValue');
	
	if(findState && Number(findState||'0')>0) queryObject.findFbState = findState;// 状态
	if(findFrom && findFrom.length>0) queryObject.findFbFrom = findFrom;// 来源
	if(findMobile && Number(findMobile||'0')>0) queryObject.findFbMobile = findMobile;// 手机
	if (findFebType && Number(findFebType ||'0')>0) queryObject.findFebType = findFebType;//反馈类型 
	
	return queryObject;
}

/**
 * 查询数据
 */
function doQuery(){
	$("#feedbackGrid").datagrid('unselectAll');
	$("#feedbackGrid").datagrid('load', getQueryParam());
}

function fmtCreateTime(value, rowData, rowIndex){
	if(!value)return "";
	var s = new Date(value);
	return rowData.create_time=$.DateUtil.format(s,'yyyy/MM/dd hh:mm:ss');
}
